{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 累计乘积\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5037569-0d80-48cb-8814-032a0f94bf73",
   "metadata": {},
   "source": [
    "该代码使用`numpy`库生成一个从$1$到$10$的等间距数组，并计算其累积积。累积积是指从数组开头依次将每个元素相乘，逐步计算出各个位置上的连乘结果，形成新的数组。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **生成等间距数组**：`np.linspace(1, 10, 10)`生成一个数组$a_i$，从$1.0$到$10.0$共包含$10$个元素，形成以下数组：\n",
    "\n",
    "   $$\n",
    "   a_i = [1.0, 2.0, 3.0, \\dots, 10.0]\n",
    "   $$\n",
    "\n",
    "2. **计算累积积**：使用`np.cumprod(a_i)`对数组$a_i$计算累积积，即从第一个元素开始依次累乘直到当前位置，构成一个新的数组$a_i\\_cumprod$。累积积的第$n$个元素定义为前$n$个元素的乘积：\n",
    "\n",
    "   $$\n",
    "   a_i\\_cumprod[n] = \\prod_{k=1}^{n} a_i[k]\n",
    "   $$\n",
    "\n",
    "   例如，累积积的前几项结果为：\n",
    "\n",
    "   $$\n",
    "   a_i\\_cumprod = [1.0, 2.0, 6.0, 24.0, \\dots, 3628800.0]\n",
    "   $$\n",
    "\n",
    "3. **输出设置**：为防止大数值以科学计数法显示，代码使用`np.set_printoptions(suppress=True)`确保输出以普通小数形式显示。\n",
    "\n",
    "### 总结\n",
    "\n",
    "通过该代码，我们可以轻松地计算数组的累积积，这在许多计算和建模场景中非常有用，例如累乘关系、概率模型等。最终输出的数组$a_i\\_cumprod$展示了累积乘积的逐步计算过程。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3a1b6fa-2a14-4dc8-be95-4b1066599f37",
   "metadata": {},
   "source": [
    "## 生成等间距数组并计算其累计乘积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b00f27e5-5dae-4bd4-8b25-ca7ce6d53478",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入numpy库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "74ac1f4e-5964-4be3-81fe-2a5734f1d40b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]\n"
     ]
    }
   ],
   "source": [
    "a_i = np.linspace(1, 10, 10)  # 创建一个从1到10等间距的数组，共10个元素\n",
    "print(a_i)  # 输出生成的数组a_i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4951aa62-327b-4008-9c61-ab51457d996d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[      1.       2.       6.      24.     120.     720.    5040.   40320.\n",
      "  362880. 3628800.]\n"
     ]
    }
   ],
   "source": [
    "a_i_cumprod = np.cumprod(a_i)  # 计算数组a_i的累积积\n",
    "np.set_printoptions(suppress=True)  # 设置打印选项，避免科学计数法显示\n",
    "print(a_i_cumprod)  # 输出累积积结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
